/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import conexiones.cConexion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author javaee
 */
public class myBean {
     public static String selectLogin =  "select count(*) as n from usuario where usuario=? and clave=?";
     public static String selectBuscar = "select restaurante,direccion,telefono  from restaurante where restaurante like ?";
     public static String selectBuscarAll = "select restaurante,direccion,telefono  from restaurante";
     
     String restaurante=null;
     String direccion=null;
     String telefono=null;
     String buscar=null;

    public String getRestaurante() {
        return restaurante;
    }

    public String getDireccion() {
        return direccion;
    }

    public String getTelefono() {
        return telefono;
    }

    public String getBuscar() {
        return buscar;
    }

    public void setRestaurante(String restaurante) {
        this.restaurante = restaurante;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }
     
     
     
     public int existe(String user, String clave) {
        int existe=-1;    
        try {
            Connection cnn = cConexion.conectar_ds();
            PreparedStatement psta= null;
            ResultSet rset = null;
            psta = cnn.prepareStatement(myBean.selectLogin);
            psta.setString(1, user);
            psta.setString(2, clave);
            rset = psta.executeQuery();
            while (rset.next()) {
               existe=rset.getInt("n");
            }
            rset.close();
            psta.close();
            cnn.close();

        } catch (Exception ex) 
        {
          ex.printStackTrace();
        }
        
        return existe;    
        
    }
     
     public List<myBean > getListado() {
     //public List<myBean > getListado() {
        List< myBean > lista = new ArrayList< myBean >(0);
        String buscar=this.getBuscar();
        try {
            Connection cnn = cConexion.conectar_ds();
            ResultSet rset = null;
            PreparedStatement psta= null;
            if(buscar.isEmpty())
            {
             psta = cnn.prepareStatement(myBean.selectBuscarAll);
            }
            else
            {
                psta = cnn.prepareStatement(myBean.selectBuscar);
                psta.setString(1, "%"+buscar+"%");
            }
            
            rset = psta.executeQuery();
            while (rset.next()) {
            myBean current = new  myBean ();
            current.setRestaurante(rset.getString("restaurante"));
            current.setDireccion(rset.getString("direccion"));
            current.setTelefono(rset.getString("telefono"));
            lista.add(current);
            }
            rset.close();
            psta.close();
            cnn.close();

        } catch (Exception ex) {
            ex.printStackTrace();  
        } finally {
            return lista;
        }
    }
}
